package com.tzbank.wisdom.esb.consumer;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.dc.eai.data.CompositeData;
import com.dcfs.esb.client.ESBClient;
import com.tzbank.esb.util.EsbConversionUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultUtil;

public class Consumer {
	
	public static Map test() {
		
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = new HashMap<String, Object>();
		//调用核心计价因子报文头入参
		
		reqSysHead.put("serviceCode", "11003000017"); // 服务码
		reqSysHead.put("serviceScene", "04"); // 服务场景
		reqSysHead.put("clientNo", "");
		reqSysHead.put("eClientNo", "");
		reqSysHead.put("userNo", "");
		reqSysHead.put("sourceType", "PMBS");			//
		reqSysHead.put("consumerSeqNo", "123456");		//
		reqSysHead.put("tranTimestamp", new Date().getTime()+"");		//
		reqSysHead.put("branchId", "");
		reqSysHead.put("sourceBranchNo", "");
		reqSysHead.put("destBranchNo", "");
		reqSysHead.put("logonType", "");			//R
		//调用核心计价因子入参
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("logonId", "220221196001123012");			//
		inputMap.put("queryFlag", "");

		// Map转换Esb报文
		CompositeData reqCompositeData = null;

		try {
			reqCompositeData = EsbConversionUtil.mapToCompositeData(inputMap);
		} catch (Exception e) {
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}

		CompositeData respCompositeData = null;
		// 调用核心服务
		try {
			respCompositeData = ESBClient.request(reqCompositeData);
		} catch (Exception e) {
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}
		//通过核心所返回标志Code=000000来判断调用核心计价因子是否调用成功
		String code = (String) respCompositeData.getStruct("SYS_HEAD").getArray("RET").getStruct(0)
				.getField("RET_CODE").getValue();
		//获取msg信息
		String msg = (String) respCompositeData.getStruct("SYS_HEAD").getArray("RET").getStruct(0)
				.getField("RET_MSG").getValue();
		if (code != null && !code.equals("000000")) {
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), null);
		}

		// Esb报文转换Map
		Map<String, Object> outputMap = new HashMap<>();
		try {
			outputMap = EsbConversionUtil.compositeDataToMap(respCompositeData);
			System.out.println(outputMap);
		} catch (Exception e) {
			return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode
					(), null);
		}
		
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), null);
	}
	
	public static void main(String[] args) {
		test();
	}

}
